﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SGAEntidades;
using SGABDUtil;
using SGAInterfaces;

namespace SGAAccesoDatos
{
    public class Proveedor : IProveedor
    {
        public IList<ProveedorInfo> Listar(ProveedorInfo oProveedor)
        {
            var sqlParm = new SqlParameter[2];
            var oListaProveedor = new List<ProveedorInfo>();

            sqlParm[0] = new SqlParameter("@Ruc_Proveedor", SqlDbType.VarChar);
            if (oProveedor.RucProveedor != null) { sqlParm[0].Value = oProveedor.RucProveedor; } else { sqlParm[0].Value = DBNull.Value; }

            sqlParm[1] = new SqlParameter("@Razon_Social", SqlDbType.VarChar);
            if (oProveedor.RazonSocial != null) { sqlParm[1].Value = oProveedor.RazonSocial; } else { sqlParm[1].Value = DBNull.Value; }

            using (var drd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringDistributedTransaction, CommandType.StoredProcedure, "SGA_SP_CONSULTAR_PROVEEDOR", sqlParm))
            {
                if (drd != null)
                {
                    if (drd.HasRows)
                    {
                        while (drd.Read())
                        {
                            oListaProveedor.Add(new ProveedorInfo(drd.GetString(0).Trim(), drd.GetString(1).Trim()));
                        }
                    }
                }
            }
            return oListaProveedor;
        }

        public ProveedorInfo Consultar(ProveedorInfo oProveedor)
        {
            var sqlParm = new SqlParameter[2];
            var oEntProveedor = new ProveedorInfo();

            sqlParm[0] = new SqlParameter("@Ruc_Proveedor", SqlDbType.VarChar);
            if (oProveedor.RucProveedor != null) { sqlParm[0].Value = oProveedor.RucProveedor; } else { sqlParm[0].Value = DBNull.Value; }

            sqlParm[1] = new SqlParameter("@Razon_Social", SqlDbType.VarChar);
            if (oProveedor.RazonSocial != null) { sqlParm[1].Value = oProveedor.RazonSocial; } else { sqlParm[1].Value = DBNull.Value; }

            using (var drd = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringDistributedTransaction, CommandType.StoredProcedure, "SGA_SP_CONSULTAR_PROVEEDOR", sqlParm))
            {
                if (drd != null)
                {
                    if (drd.HasRows)
                    {
                        drd.Read();
                        oEntProveedor = new ProveedorInfo(drd.GetString(0).Trim(), drd.GetString(1).Trim());
                    }
                }
            }
            return oEntProveedor;
        }
    }
}
